package org.jetnuke.modules.blog;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogPostDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.BlogCategory;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.modules.blog.utils.BlogUtils;

public class Index implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Index.class);

	int id;
	int cat;

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (!BlogUtils.isBlogInitialized(id)) {

			ActionForward returnActionForward = new ActionForward(info,
					"no_blog.vm");
			if (logger.isDebugEnabled()) {
				logger
						.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
			}
			return returnActionForward;
		}
		net.jforum.entities.User usr = DataAccessDriver.getInst().newUserDAO()
				.selectById(id);

		BlogCategoryDAO bcm = DataAccessDriver.getInst().newBlogCategoryDAO();
		BlogPostDAO bpm = DataAccessDriver.getInst().newBlogPostDAO();
		List categoryList = bcm.selectByUser(id);
		boolean catFound = false;
		Iterator it = categoryList.iterator();
		BlogCategory showCategory = null;
		if (cat != 0) {
			while (it.hasNext()) {
				BlogCategory bc = (BlogCategory) it.next();
				if (bc.getID() == cat) {
					catFound = true;
					showCategory = bc;
					break;
				}
			}
			if (!catFound)
				cat = 0;
		}

		List blogList = null;
		if (cat == 0)
			blogList = bpm.retrievePublishByUser(id);
		else
			blogList = bpm.retrievePublishByUserCat(id, cat);
		vc.put("blogList", blogList);

		if (ui != null && ui.getID() == id) {
			vc.put("my_blog", Boolean.TRUE);
		}

		HashMap cp = new HashMap();
		vc.put("cp", cp);
		it = categoryList.iterator();
		while (it.hasNext()) {
			BlogCategory bc = (BlogCategory) it.next();
			cp.put(new Integer(bc.getID()), bc);
		}
		BlogUserSettingsDAO busm = DataAccessDriver.getInst()
				.newBlogUserSettingsDAO();
		BlogUserSettings bus = busm.retrieveByPk(usr.getId());
		vc.put("settings", bus);
		vc.put("_pagetitle", " - " + bus.getTITLE());
		if (showCategory != null) {
			vc.put("showCategory", showCategory);
		}
		List preLeftBlocks = new ArrayList();
		preLeftBlocks.add(BlogUtils.createUserBlockInfo(vc, usr));
		preLeftBlocks.add(BlogUtils.createCalendarBlock(vc,
				new GregorianCalendar(), id));
		BlogUtils.manageStyle(bus, vc, preLeftBlocks);
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	public void setCat(int cat) {
		this.cat = cat;
	}

	public void setId(int id) {
		this.id = id;
	}

}
